import store from "@/store";
import utils from "@/utils";
import formatRichText from "@/utils/formatRichText";

export default {
  data() {
    return {
      options: {
        yesnoBool: [
          {
            name: "是",
            code: true,
          },
          {
            name: "否",
            code: false,
          },
        ],
        yesno: [
          {
            name: "是",
            code: 1,
          },
          {
            name: "否",
            code: 2,
          },
        ],
        whether: [
          {
            name: "是",
            code: 1,
          },
          {
            name: "否",
            code: 0,
          },
        ],
        recommend: [
          {
            name: "推荐",
            code: 1,
          },
          {
            name: "不推荐",
            code: 2,
          },
        ],
        publishStatus: [
          {
            name: "已发布",
            code: 1,
          },
          {
            name: "未发布",
            code: 2,
          },
        ],
        pushStatus: [
          {
            name: "已上架",
            code: 1,
          },
          {
            name: "未上架",
            code: 2,
          },
        ],
        approveStatus: [
          {
            name: "待审核",
            code: 0,
          },
          {
            name: "已审核",
            code: 1,
          },
          {
            name: "已驳回",
            code: 2,
          },
        ],
        // 跳转方式
        forwordWay: [
          {
            name: "内部",
            code: 1,
          },
          {
            name: "外部",
            code: 2,
          },
        ],
        // 跳转类型
        forwordType: [
          {
            name: "农产品",
            code: 1,
          },
          {
            name: "农事服务",
            code: 2,
          },
          {
            name: "内容管理",
            code: 3,
          },
          {
            name: "APP菜单",
            code: 4,
          },
          {
            name: "专题页",
            code: 5,
          },
        ],
        // 登录渠道
        channel: [
          {
            name: "PC",
            code: 1,
          },
          {
            name: "APP",
            code: 2,
          },
        ],
        // 操作类型
        operateType: [
          {
            name: "登录",
            code: 1,
          },
          {
            name: "登出",
            code: 2,
          },
        ],
        shipmentStatus: [
          {
            name: "已发货",
            code: 1,
          },
          {
            name: "未发货",
            code: 2,
          },
        ],
      },
    };
  },
  methods: {
    // options状态渲染
    renderOption(value, field) {
      if (!this.options) {
        console.log("请配置mixins options");
        return false;
      }

      let statusText = "";
      this.options[field].map((item) => {
        if (item.code == value) {
          statusText = item.name;
        }
      });

      return statusText;
    },

    // 时间截取
    timeChange(date, target, field) {
      if (!field) {
        field = ["searchBeginTime", "searchEndTime"];
      }

      if (date) {
        target[field[0]] = utils
          .dayjsIns(date[0])
          .format("YYYY/MM/DD hh:mm:ss");
        target[field[1]] = utils
          .dayjsIns(date[1])
          .format("YYYY/MM/DD hh:mm:ss");
      } else {
        target[field[0]] = null;
        target[field[1]] = null;
      }
    },

    // 删除 批量删除
    async batchDelete(url, idField, ids) {
      if (!ids) {
        ids = this.$refs.mainTable.getCheckData().map((c) => c[idField]);

        if (!ids || ids.length == 0) {
          this.$message({
            type: "warning",
            message: "请勾选数据",
          });
          return;
        }

        this.$confirm(`确认删除?`).then(() => {
          url(ids).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.query();
          });
        });
      } else {
        this.$confirm(`确认删除?`).then(() => {
          url(ids).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.query();
          });
        });
      }
    },
    formatRichText(content) {
      return formatRichText(content);
    },
  },
};
